package org.algorithm.tengxun;
/**
 * 示例：
 * 输入："Let's take LeetCode contest"
 * 输出："s'teL ekat edoCteeL tsetnoc"
 *
 */
public class ReverseWordsInString {
    public static String reverseWords(String s){
        String[] words = s.split(" ");
        for (int i = 0; i < words.length; i++) {
            words[i]=reverseStr(words[i]);
        }
        StringBuilder result = new StringBuilder();
        for (String word: words){
            result.append(word).append(" ");
        }
        return result.toString().trim();
    }

    public static String reverseStr(String s){
        char[] charArray = s.toCharArray();
        int left=0,right=charArray.length-1;
        while (left<right){
            char temp = charArray[left];
            charArray[left]=charArray[right];
            charArray[right]=temp;
            left++;
            right--;
        }
        return new String(charArray);
    }

    public static void main(String[] args) {
        String str ="Let's take LeetCode contest";
        System.out.println(reverseWords(str));
    }
}
